import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";
// import store from '@/store'
// import  getters  from '@/store/getters'
import { getUserType } from "@/utils/auth";
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// alert(getUserType())
const userType = getUserType();
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
  {
    path: "/register",
    name: "register",
    component: () => import("@/views/login/register"),
    hidden: true,
  },
  // 复杂表头的导出
  {
    path: "/complexHeaderExport",
    component: () => import("../views/complexheaderExport/exportDemo.vue"),
    hidden: true,
  },
  // 导入
  {
    path: "/import",
    name: "import",
    component: () => import("../views/import/index.vue"),
    hidden: true,
  },
  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },

  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/dashboard/index"),
        meta: { title: "首页", icon: "dashboard" },
      },
    ],
  },
  // 写入
  {
    path: "/write",
    name: "write",
    component: () => import("@/views/dashboard/component/doctor"),
    hidden: true,
    meta: { title: "写入", icon: "patient" },
  },
  // 动画写入
  {
    path: "/animate",
    name: "animate",
    component: () => import("@/views/login/animate.vue"),
    hidden: false,
    meta: { title: "动画", icon: "patient" },
  },
  // {
  //   path: '/patient',
  //   component: Layout,
  //   redirect: '/patient/index',
  //   name: '患者信息',
  //   meta: { title: '患者信息', icon: 'el-icon-s-help' },
  //   children: [
  //     {
  //       path: 'index',
  //       name: 'index',
  //       component: () => import('@/views/patient/index'),
  //       meta: { title: '患者', icon: 'form' }
  //     },
  //     {
  //       path: 'doctor',
  //       name: 'doctor',
  //       component: () => import('@/views/doctor/index'),
  //       meta: { title: '医生', icon: 'form' }
  //     }
  //   ]
  // },

  // {
  //   path: '/patient',
  //   name: 'patient',
  //   component: () => import('@/views/patient/index')
  // },

  // 患者
  {
    path: "/patient1",
    component: Layout,
    children: [
      {
        path: "patient",
        name: "patient",
        component: () => import("@/views/patient/index"),
        // hidden: getters.userType !== "patient" ? false : true,
        hidden: userType !== "patient",
        // hidden: false,
        meta: { title: "患者", icon: "patient" },
      },
    ],
  },
  // 管理员
  {
    path: "/admin/patient",
    component: Layout,
    children: [
      {
        path: "/admin/patient",
        name: "patient",
        component: () => import("@/views/admin/components/patient"),
        // hidden: userType !== 'admin',
        hidden: true,
        meta: { title: "患者管理", icon: "form" },
      },
    ],
  },
  {
    path: "/admin/doctor",
    component: Layout,
    children: [
      {
        path: "doctor",
        name: "doctor",
        component: () => import("@/views/admin/components/doctor"),
        hidden: userType !== "admin",
        meta: { title: "医生管理", icon: "form" },
      },
    ],
  },
  {
    path: "/admin/department",
    component: Layout,
    children: [
      {
        path: "department",
        name: "department",
        component: () => import("@/views/admin/components/department"),
        hidden: userType !== "admin",
        meta: { title: "科室管理", icon: "form" },
      },
    ],
  },
  {
    path: "/admin/clinic",
    component: Layout,
    children: [
      {
        path: "/clinic",
        name: "clinic",
        component: () => import("@/views/admin/components/clinic"),
        hidden: userType !== "admin",
        meta: { title: "诊室管理", icon: "form" },
      },
    ],
  },

  // 医生
  {
    path: "/doctor",
    component: Layout,
    children: [
      {
        path: "doctor",
        name: "doctor",
        component: () => import("@/views/doctor"),
        hidden: userType !== "doctor",
        meta: {
          title: "医生",
          icon: "el-icon-s-custom",
        },
      },
    ],
  },
  {
    path: "/department",
    component: Layout,
    children: [
      {
        path: "department",
        name: "department",
        component: () => import("@/views/department"),
        hidden: userType !== "doctor",
        meta: {
          title: "科室",
          icon: "keshi",
        },
      },
    ],
  },

  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
